package com.ansion.commonchat.contact.core;

import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;

import com.ansion.commonchat.contact.bean.ContactBaseBean;

import java.util.List;


public class ContactDiffCallback extends DiffUtil.Callback {

    private final List<ContactBaseBean> mOldList;
    private final List<ContactBaseBean> mNewList;

    public ContactDiffCallback(@NonNull List<ContactBaseBean> oldList, @NonNull List<ContactBaseBean> newList) {
        this.mOldList = oldList;
        this.mNewList = newList;
    }

    @Override
    public int getOldListSize() {
        return mOldList.size();
    }

    @Override
    public int getNewListSize() {
        return mNewList.size();
    }

    @Override
    public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
        ContactBaseBean oldItem = mOldList.get(oldItemPosition);
        ContactBaseBean newItem = mNewList.get(newItemPosition);
        if (oldItem == null || newItem == null) {
            return false;
        }
        return TextUtils.equals(oldItem.getUniqueTag(), newItem.getUniqueTag());
    }

    @Override
    public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
        ContactBaseBean oldItem = mOldList.get(oldItemPosition);
        ContactBaseBean newItem = mNewList.get(newItemPosition);
        if (oldItem == null || newItem == null) {
            return false;
        }
        return oldItem.equals(newItem);
    }
}
